/***********************************************************************
 * Module:  Doctor.java
 * Author:  Vanil
 * Purpose: Defines the Class Doctor
 ***********************************************************************/
package Personal;
import DB.dao.*;
import DB.dto.*;
import DB.exceptions.*;
import DB.factory.OperationDaoFactory;
import DB.jdbc.*;
import Process.*;
import Hosp.Department;
import DB.factory.DoctorHasOperatinsDaoFactory;
import org.simpleframework.xml.Root;

import java.lang.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

@Root
public class Doctor extends Person {
    public Doctor(DoctorDto dto) {
        super(dto);
    }


//    @Override
//    public int hashCode() {
//        return (new Integer( this.getIdPerson())).hashCode();    //To change body of overridden methods use File | Settings | File Templates.
//    }

    public void viewAllTasks() {
      // TODO: implement

    }
   
   public void examine(Person patient) {
      // TODO: implement
   }
   
   public int addPatient() {
      // TODO: implement
      return 0;
   }
   
   public void showQuery() {
      // TODO: implement
   }
   
   public Prescription createNewPrescription(Patient patient) {
      // TODO: implement
      return null;
   }

    public double getExp() {
        return ((DoctorDto)dto).getExp();
    }

    public String getSpeciality() {
        return ((DoctorDto)dto).getSpeciality();
    }
   /** @pdGenerated default getter */
   public java.util.Collection<Patient> getPatients() throws PersonDaoException, PatientDaoException {
       int idDoctor = ((DoctorDto)dto).getIdDoctor();
       PatientDao dao = new PatientDaoImpl();
       PatientDto[] patientDtos = dao.findByDoctor(idDoctor);
       Collection<Patient> patients = new HashSet<Patient>();
       for(PatientDto patDto:patientDtos)
       {
           patients.add(new Patient(patDto));
       }
      return patients;
   }
   public boolean hasPatient(int pk) throws PatientDaoException, PersonDaoException {
       Collection<Patient> patientCollection = getPatients();
       for(Patient pat :patientCollection)
       {
           if(pat.getIdPerson()==pk)
               return true;
       }
       return false;
       // return patientCollection.contains(patDto);
   }
    public boolean hasIntake(int pk) throws IntakeDaoException {
       Collection<Intake> intakeCollection = getIntakes();
       for(Intake intake : intakeCollection)
       {
           if(intake.getId()==pk)
               return true;
       }
       return false;
       // return intakeCollection.contains(patDto);
   }

   /** @pdGenerated default add
     * @param newPatient */
   public void addPatient(Patient newPatient) throws PatientDaoException {
       if(newPatient!=null)
         newPatient.setDoctor(this);
   }
   
   /** @pdGenerated default remove
     * @param oldPatient */
   public void removePatient(Patient oldPatient) throws PatientDaoException {
      if (oldPatient != null)
            oldPatient.setDoctor((Doctor)null);

   }

   /** @pdGenerated default getter */
   public java.util.Collection<Intake> getIntakes() throws IntakeDaoException {
       int idDoctor = ((DoctorDto)dto).getIdDoctor();
       IntakeDao dao = new IntakeDaoImpl();
       IntakeDto[] intakeDtos = dao.findWhereIdDoctorEquals(idDoctor);
       Collection<Intake> intakes = new ArrayList<Intake>();
       for(IntakeDto intDto:intakeDtos)
       {
           intakes.add(new Intake(intDto));
       }
      return intakes;
   }

   
   /** @pdGenerated default add
     * @param newIntake */
   public void addIntake(Intake newIntake) throws IntakeDaoException {
       IntakeDao dao = new IntakeDaoImpl();
       dao.insert(newIntake.dto);
   }

   /** @pdGenerated default remove
     * @param oldIntake */
   public void removeIntake(Intake oldIntake) throws IntakeDaoException {
      IntakeDao dao = new IntakeDaoImpl();
       dao.delete(oldIntake.getId());
   }

   /** @pdGenerated default getter */
   public java.util.Collection<Operation> getOperations() throws OperationDaoException, DoctorHasOperatinsDaoException {
       DoctorHasOperationsDao docOperDao = DoctorHasOperatinsDaoFactory.create();
       DoctorHasOperatins [] operationDtos = docOperDao.findByDoctor(this.dto.getIdPerson());
       java.util.Collection<Operation> opList = new ArrayList<Operation>();
       for(DoctorHasOperatins dto :operationDtos)
       {
           OperationDao dao = OperationDaoFactory.create();
           OperationDto operationDto = dao.findByPrimaryKey(dto.getIdOperation());
           opList.add(new Operation(operationDto));
       }
       return opList;
   }
   /** @pdGenerated default parent getter */
   public Department getDepartment() throws DepartmentDaoException {
       int idDepartment = ((DoctorDto) dto).getIdDepartment();
       DepartmentDao dao = new DepartmentDaoImpl();
       return new Department(dao.findByPrimaryKey(idDepartment));

   }

   /** @pdGenerated default parent setter
     * @param newDepartment */
   public void setDepartment(Department newDepartment) throws PersonDaoException {
      ((DoctorDto)this.dto).setIdDepartment(newDepartment.getIdDepartment());

   }

    public void setExp(int exp) throws DoctorDaoException {
        DoctorDao dao = new DoctorDaoImpl();
       ((DoctorDto)dto).setExp(exp);
    }

    public void setSpeciality(String speciality) throws DoctorDaoException {
       ((DoctorDto)dto).setSpeciality(speciality);
    }

    @Override
    public String toString() {
        return dto.getName();    //To change body of overridden methods use File | Settings | File Templates.
       // return "Doctor "+this.name+" exp: "+ this.exp + " Speciality "+this.speciality;
    }
}